package com.fyzk.activity.lottery.recommend_order;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.popupwindow.FollowDocumentaryPopupWindow;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.IntentParamUtils;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanxs11 on 17/7/4.
 * 晒单大厅Adapter
 */

public class RecommendOrderHallAdapter extends BaseAdapter {

    private Context context;
    private List<MyRecommendOrderBean> infos = new ArrayList<>();

    public RecommendOrderHallAdapter(Context context, List<MyRecommendOrderBean> infos) {
        this.context = context;
        this.infos = infos;
    }

    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public Object getItem(int position) {
        return infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final MyRecommendOrderBean info = infos.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_recommendorderhall, null);
            holder.item_ll = (LinearLayout) convertView.findViewById(R.id.rr_hall_item_ll);
            holder.userInfor_ll = (LinearLayout) convertView.findViewById(R.id.rr_hall_item_userinfor_ll);
            holder.userName_tv = (TextView) convertView.findViewById(R.id.rr_hall_item_username);
            holder.time_tv = (TextView) convertView.findViewById(R.id.rr_hall_item_time);
//            holder.m_Zhong_n_tv = (TextView) convertView.findViewById(R.id.rr_hall_item_m_n);
            holder.endTime_tv = (TextView) convertView.findViewById(R.id.rr_hall_item_endtime);
            holder.type_tv = (TextView) convertView.findViewById(R.id.rr_hall_item_type);
            holder.content_ll = (LinearLayout) convertView.findViewById(R.id.rr_hall_item_content_ll);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (info.getIsexpert() == 1) {
            Drawable drawable = context.getResources().getDrawable(R.drawable.rr_v_icon);
            holder.userName_tv.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
        } else {
            holder.userName_tv.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }
        holder.userName_tv.setText(PublicMethod.getSenderName("", info.getUserName()));
//        holder.time_tv.setText(info.getReceiveTime());
        holder.time_tv.setText(Html.fromHtml("命中：<font color = '#FD4A4A'>" + info.getTotalCount() + "中" + info.getWinCount() + "</font>"));
//        holder.m_Zhong_n_tv.setText(info.getTotalCount()+"中"+info.getWinCount());
        holder.endTime_tv.setText("截止日期：" + info.getEndTime());

        if (info.getWay() != 0) {   // 方案保密
            holder.type_tv.setVisibility(View.VISIBLE);
            holder.content_ll.setVisibility(View.GONE);
        } else {
            holder.type_tv.setVisibility(View.GONE);
            holder.content_ll.setVisibility(View.VISIBLE);
            MyRecommendOrderBean.DetailBean detailBean = info.getDetail();
            setContent(holder.content_ll, detailBean.getRaceBetcode(), detailBean.getPlayName());
        }

        holder.userInfor_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HisCenterActivity.class);
                if (info != null) {
                    intent.putExtra(IntentParamUtils.USERNO, info.getUserno());
                    intent.putExtra("isExpert", info.getIsexpert());
                }
                context.startActivity(intent);
            }
        });

        holder.item_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(context, RecommendDetailActivity.class);
//                intent.putExtra(IntentParamUtils.RECOMMENDINFOR, info);
//                context.startActivity(intent);
                showFollowDocumentaryPopup(info);
            }
        });
        return convertView;
    }

    FollowDocumentaryPopupWindow followDocumentaryPopupWindow;

    /**
     * 展示跟单的弹窗
     *
     * @param item 跟单内容
     */
    private void showFollowDocumentaryPopup(MyRecommendOrderBean item) {
        if (null == item)
            return;
        followDocumentaryPopupWindow = new FollowDocumentaryPopupWindow(context, item);
        followDocumentaryPopupWindow.showPopupWindow();
    }

    private void setContent(LinearLayout content_ll, List<MyRecommendOrderBean.DetailBean.RaceBetcodeBean> raceInfos, String playName) {
        content_ll.removeAllViews();
        for (int i = 0; i < raceInfos.size(); i++) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.view_rr_hall_content, null);
            ImageView left_iv = (ImageView) itemView.findViewById(R.id.rr_hall_content_left_iv);
            TextView line_tv = (TextView) itemView.findViewById(R.id.rr_hall_content_line_tv);
            TextView matchId_tv = (TextView) itemView.findViewById(R.id.rr_hall_content_matchid);
            TextView matchName_tv = (TextView) itemView.findViewById(R.id.rr_hall_content_matchname);
            TextView homeAndAway_tv = (TextView) itemView.findViewById(R.id.rr_hall_content_h_a);
            TextView betCode_tv = (TextView) itemView.findViewById(R.id.rr_hall_content_betcode_tv);

            MyRecommendOrderBean.DetailBean.RaceBetcodeBean raceInfo = raceInfos.get(i);
            matchId_tv.setText(raceInfo.getOfficialWeekDay() + raceInfo.getMatchNum());
            matchName_tv.setText(raceInfo.getMatchName());
            homeAndAway_tv.setText(raceInfo.getHomeTeam() + "\n VS \n" + raceInfo.getAwayTeam());
            String betCodes = "";   // 投注内容
            String results = "";    // 比赛结果
            for (int j = 0; j < raceInfo.getContents().size(); j++) {
                betCodes += raceInfo.getContents().get(j).getBetcode() + " ";
            }
            betCode_tv.setText(Html.fromHtml(betCodes, null, null));


//            if (playName.equals("2串1")) {
//                left_iv.setImageDrawable(context.getResources().getDrawable(R.drawable.rr_2c1_icon));
//            } else if (playName.equals("3串1")) {
//                left_iv.setImageDrawable(context.getResources().getDrawable(R.drawable.rr_3c1_icon));
//            } else if (playName.equals("单关")){
//                left_iv.setImageDrawable(context.getResources().getDrawable(R.drawable.rr_dan_icon));
//            } else {
//                left_iv.setImageDrawable(context.getResources().getDrawable(R.drawable.rr_23c1_icon));
//            }
//            if (i == 0) {
//                left_iv.setVisibility(View.VISIBLE);
//            } else {
//                left_iv.setVisibility(View.GONE);
//            }
            if (i == raceInfos.size() - 1) {
                line_tv.setVisibility(View.GONE);
            } else {
                line_tv.setVisibility(View.VISIBLE);
            }
            content_ll.addView(itemView);
        }
    }

    public void setInfos(List<MyRecommendOrderBean> infos) {
        this.infos = infos;
        this.notifyDataSetChanged();
    }

    class ViewHolder {
        LinearLayout item_ll;
        LinearLayout userInfor_ll;
        TextView userName_tv;
        TextView time_tv;
        TextView m_Zhong_n_tv;
        TextView endTime_tv;
        TextView type_tv;
        LinearLayout content_ll;   // 比赛信息

    }
}
